<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2018/7/4
 * Time: 15:54
 */

namespace Advertisement\Controller;


use Common\Controller\AdminbaseController;
use Common\Model\InformationModel;
use Common\Model\InfoTypeModel;

class InfoController extends AdminbaseController
{
    private $information_model;
    private $info_type_model;

    public function __construct()
    {
        parent::__construct();
        $this->information_model = new InformationModel();
        $this->info_type_model   = new InfoTypeModel();
    }

    public function news() {
        $this->_lists(InformationModel::TAG_NEW);
    }

    public function edu() {
        $this->_lists(InformationModel::TAG_EDU);
    }

    public function _lists($tag) {

        //搜索条件
        $where['tag'] = $tag;
        if(IS_POST) {
            $title = I('post.title');
            if($title) {
                $where['title'] = array('like', '%'.$title.'%');
            }

        } else {
            $title = I('get.title');
            if($title) {
                $where['title'] = array('like', '%'.$title.'%');
            }

        }

        $count = $this->information_model
            ->where($where)
            ->count();
        $page = $this->page($count, C('PAGE_NUMBER'));
        $result = $this->information_model
            ->where($where)
            ->field('title, create_time, id, cover, sort_order,type, if_hot, read')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order('id desc')
            ->select();

        if($tag == InformationModel::TAG_NEW) {
            foreach($result as $k => $v) {
                $result[$k]['str_manage'] .= '<a class="js-ajax-btn-dialog" href="' . U('Info/edit', ['id' => $v['id'], 'tag' => $v['tag']]) . '">编辑</a> | <a class="js-ajax-btn-dialog js-ajax-delete" href="' . U('Info/delete', ['id' => $v['id']]) . '">删除</a>';
                $str = '<img title="点击查看原图" name="' . setUrl($v['cover']) . '" src="' . setUrl($v['cover']) . '?imageView2/2/h/30" onclick="parent.image_preview_dialog(this.name);">';

                if($v['if_hot'] == 'yes') {
                    /*$hot = '<div class="checkbox">
                        <label>
                          <input type="checkbox" name="if_hot"   id="'.$v['id'].'" checked value="no"/>推荐
                        </label>
                       </div>';*/
                    $hot = '已推荐| <a class="js-ajax-dialog-btn" href="' . U('Info/setHot', ['id' => $v['id'], 'tag_name' => 'if_hot', 'tag_value' => 'no']) . '">取消推荐</a>';

                } else {
                    /*$hot = '<div class="checkbox">
                        <label>
                          <input type="checkbox" name="if_hot"  id="'.$v['id'].'"  value="yes"/>推荐
                        </label>
                       </div>';*/
                    $hot = '未推荐| <a class="js-ajax-dialog-btn" href="' . U('Info/setHot', ['id' => $v['id'], 'tag_name' => 'if_hot', 'tag_value' => 'yes']) . '">推荐</a>';
                }

                $lists .= '<tr>
                <td>
                    <input type="checkbox" name="checkItem" info_id="'.$v['id'].'" value="'.$v['id'].'"/>
               </td>
               <td>' . ($k + 1) . '</td>
               <td>' . ($v['id']) . '</td>
               <td>' . $this->info_type_model->getTypeName($v['type']) . '</td>
               <td>' . '<a class="js-ajax-btn-dialog" href="' . U('Info/detail', ['id' => $v['id']]) . '">'.$v['title'].'</a>' . '</td>
               <td>'.$str.'</td>
               <td>'.$v['create_time'] . '</td>
               <td>'.$v['read'] . '</td>
               <td>'.$hot . '</td>
               <td>' . $result[$k]['str_manage'] . '</td>
           </tr>';

            }
        } else {
            foreach($result as $k => $v) {
                $result[$k]['str_manage'] .= '<a class="js-ajax-btn-dialog" href="' . U('edit', ['id' => $v['id'], 'tag' => $v['tag']]) . '">编辑</a> | <a class="js-ajax-btn-dialog js-ajax-delete" href="' . U('Info/delete', ['id' => $v['id']]) . '">删除</a>';
                $str = '<img title="点击查看原图" name="' . setUrl($v['cover']) . '" src="' . setUrl($v['cover']) . '?imageView2/2/h/30" onclick="parent.image_preview_dialog(this.name);">';

                if($v['if_hot'] == 'yes') {
                    /*$hot = '<div class="checkbox">
                        <label>
                          <input type="checkbox" name="if_hot"   id="'.$v['id'].'" checked value="no"/>推荐
                        </label>
                       </div>';*/
                    $hot = '已推荐| <a class="js-ajax-dialog-btn" href="' . U('Info/setHot', ['id' => $v['id'], 'tag_name' => 'if_hot', 'tag_value' => 'no']) . '">取消推荐</a>';

                } else {
                    /*$hot = '<div class="checkbox">
                        <label>
                          <input type="checkbox" name="if_hot"  id="'.$v['id'].'"  value="yes"/>推荐
                        </label>
                       </div>';*/
                    $hot = '未推荐| <a class="js-ajax-dialog-btn" href="' . U('Info/setHot', ['id' => $v['id'], 'tag_name' => 'if_hot', 'tag_value' => 'yes']) . '">推荐</a>';
                }

                $lists .= '<tr>
                       <td>
                            <input type="checkbox" name="checkItem" info_id="'.$v['id'].'" value="'.$v['id'].'"/>
                       </td>
                       <td>' . ($k + 1) . '</td>
                       <td>' . ($v['id']) . '</td>
                       <td>' . '<a class="js-ajax-btn-dialog" href="' . U('Info/detail', ['id' => $v['id']]) . '">'.$v['title'].'</a>' . '</td>
                       <td>'.$str.'</td>
                       <td>'.$v['create_time'] . '</td>
                       <td>'.$v['read'] . '</td>
                       <td>'.$hot . '</td>
                       <td>' . $result[$k]['str_manage'] . '</td>
                   </tr>';

            }
        }

        $this->assign('lists', $lists);
        $this->assign('formget', I(''));
        $this->assign('Page',$page->show());
        $this->assign('tag', $tag);
        $this->display();
    }

    /**
     * 添加
     */
    public function add() {
        $tag = I('get.tag');
        if(IS_POST) {
            $post                   = I('post.post');
            $data['discribe']       = $post['discribe'];
            $data['content']        = htmlspecialchars_decode($post['content']);
            $data['title']          = $post['title'];
            $data['source']         = $post['source'];
            $data['create_time']    = date('Y-m-d H:i:s');
            $data['cover']          = $post['cover'];
            $data['type']           = $post['type'];
            $data["virtual"]        = $post["virtual"];
            $data["tag"]            = $post["tag"];

            if($this->information_model->add($data) == false) {
                $this->error();
            } else {
                if($post["tag"] == InformationModel::TAG_NEW ){
                    $this->success('', U('news'));
                } else {
                    $this->success('', U('edu'));
                }

            }

        } else {
            $this->assign('tag', $tag);
            $this->assign('options', $this->info_type_model->getTypeOption());
            $this->display();
        }
    }


    /**
     * 详情
     */
    public function detail() {
        $id = I('get.id');
        $info = $this->information_model->find($id);
        $info['cover'] = setUrl($info['cover']);

//        $images = json_decode($info['smeta'], true);
        /*foreach(json_decode($info['smeta'], true) as $k => $v) {
            $images[] = setUrl($v['url']);
        }*/
//        var_dump($info['cover']);

        $this->assign('info', $info);
//        $this->assign('images', $images);
        $this->display();
    }

    /**
     * 编辑
     */
    public function edit() {
        $id = I('get.id');
        $info = $this->information_model->where(['id' => $id])->find();

        if(IS_POST) {
            $id = I('post.id');

            $post                   = I('post.post');
            $data['id']             = $post['id'];
            $data['discribe']       = $post['discribe'];
            $data['content']        = htmlspecialchars_decode($post['content']);
            $data['title']          = $post['title'];
            $data['source']         = $post['source'];
            $data['update_time']    = date('Y-m-d H:i:s');
            $data['cover']          = $post['cover'];
            $data['type']           = $post['type'];
            $data["virtual"]        = $post["virtual"];
            $data["tag"]            = $post["tag"];

//            dump($data);die;
            $rst = $this->information_model->save($data);
            if($rst) {
                if($post["tag"] == InformationModel::TAG_NEW ){
                    $this->success('', U('news'));
                } else {
                    $this->success('', U('edu'));
                }
            } else {
//                dump($this->information_model->getLastSql());die;
                $this->error();
            }
        } else {
            $this->assign($info);
            $this->assign('options', $this->info_type_model->getTypeOption($info['type']));
            $this->display();
        }
    }

    /**
     * 删除
     */
    public function delete() {
        $id = I('get.id');
        if($this->information_model->where(['id' => $id])->delete() == false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    /**
     * 全选删除
     */
    public function delAll() {
        $ids = I('post.info_id');//dump($ids);die;
        if($this->information_model->where(['id' => ['in', $ids]])->delete()) {
            echo json_encode(['ret' => '200', 'msg' => '成功']);
        } else {
            echo json_encode(['ret' => '210', 'msg' => '失败']);
        }
    }

    /**
     *
     */
    public function setHot() {
        $id         = I('get.id');
        $tag_name   = I('get.tag_name');
        $tag_value  = I('get.tag_value');

        if($this->information_model->where(['id' => $id])->save([$tag_name => $tag_value, 'update_time' => getTime(), 'hot_time' => getTime()])){
            $this->success();
        } else {
            $this->error('操作失败');dump($this->information_model->getLastSql());
        }
    }


}